package com.ufaud.ais.pub.webapp.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;

import com.opensymphony.xwork2.Action;
import com.ufaud.ais.pub.exception.UserNotFoundException;
import com.ufaud.ais.pub.view.JsonObjectSupport;
import com.ufaud.ais.sys.view.EmployeeView;

public class BaseAction implements Action, JsonObjectSupport {
	
	protected Map<String, String> jsonObject = new HashMap<String, String>();

	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	protected Authentication getAuth() {
		return SecurityContextHolder.getContext().getAuthentication();
	}

	/**
	 * 功能: 取得当前登录的用户
	 * 
	 * @author 林可 2010-6-24 下午11:43:09
	 * @return
	 */
	protected EmployeeView getCurrentUser() {
		Authentication auth = getAuth();
		if (auth == null)
			throw new UserNotFoundException("auth = null");
		else {
			EmployeeView currentUser = (EmployeeView) auth.getPrincipal();
			return currentUser;
		}
	}

	public String execute() throws Exception {
		return SUCCESS;
	}

	public void addJsonObjectProperty(String name, String value) {
		jsonObject.put(name, value);
	}

	public Map<String, String> getJsonObject() {
		return jsonObject;
	}

	public void removeJsonObjectProperty(String name) {
		jsonObject.remove(name);
	}

}
